<?php 
include 'settings.php';


if (isset($_POST['cat']) && isset($_FILES['torrent'])) {
	upploadTorrent();
	
}
//form perser

function upploadTorrent()  {
	//sets up some varibals
	global $torrentDir, $basDir, $message;
	$categori = $_POST['cat'];

//Target for downloaded files
$targetPath = '' . $basDir . '/' . $categori . '/';

//check so that the upploaded file is a torrent file
if ($_FILES['torrent']['type'] == "application/x-bittorrent") {
	
	//upploades the file
	if (move_uploaded_file($_FILES['torrent']['tmp_name'], '' . $torrentDir . '' . $_FILES['torrent']['name'] . '')) {
		
		//print a message if the uppload is sucsessful
		$message .= 'the file ' . $_FILES['torrent']['name']. ' has been upploaded. Download should start soon';
		$message .= "<br>";
		startTorrent('' . $torrentDir . '' . $_FILES['torrent']['name'] . '', $targetPath);
		
		
	}
}
else {
	$message .= "<br> error: you should uppload a torrent file";
}
}


function startTorrent($torrentDir, $downloadDir) {
	include_once 'settings.php';
	global $userName, $password, $host, $port, $baseDir, $message;
	
	// where the acurlly data will me downloaded

	$exec = 'transmission-remote  ' . $host . ':' . $port . ' -a ' . $torrentDir . ' -w ' .  $downloadDir . ' -n ' . $userName . ':' . $password . '';
	$startTorrent = shell_exec($exec);
	echo $startTorrent;
	echo '<br>';

}

class getDirs {
	
	//get all the categorys from the names of the folders in $basDir
	 	public function cats() {
			
			$counter = 0;
			global $basDir;
			$cats = array();
			
			if (is_dir($basDir)) {
		    if ($dh = opendir($basDir)) {
		        while (($file = readdir($dh)) !== false) {
						// there is probobly a nicer way to do this without the counter		           
		           $this->cats[$counter]=$file;
		           $counter++;
		        }
		        $counter=0;
		        closedir($dh);
		        //removes the dirs that shoulden't be counted as a directory
		        foreach ($this->cats as $cat) {
		        	 if ($cat == ".." || $cat == "." || $cat == "." || $cat =="lost+found") {
		        		unset($this->cats[$counter]);
		        		$counter++;
		        		
		        	}
		        	else {
		        		
		        		$counter++;
		        		
		        	}
		        	
		        }

		    }
		}
		
	}
		//creates the select menu 
		public function createSelectOptions() {
			
			foreach ($this->cats as $cat) {
				$this->printCats .='<option name="cat">' . $cat . '</option>';
			
		}
		echo $this->printCats;
	}
}
	



$getDirs = new getDirs();
$getDirs->cats();
?>


<html>
	<form action="index.php" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td>
					Chose which file that should be upploaded:
				
				</td>
				
				<td>
					<input type="file" name="torrent"> 
					<br>
				</td>
			</tr>
			<tr>
				<td>
					Chose which category the file should be upploaded to:
				
				</td>
				<td>
					<select name="cat">
						<?php $getDirs->createSelectOptions(); ?>
					
					</select> 
				</td>
			</tr>
			<tr>
			<td>
			<input type="submit">
			</td>
			</tr>
			<tr>
			<td>
			<?php
			include 'settings.php';
			echo $message ?>
			</td>
			</tr>
		
		</table>
	</form>

</html>